package org.madmac.join;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;

import org.madmac.conn.Conn;

@Deprecated
public class Password {
	
	private String passwd;
	private Date data_creation;
	private Date data_remove;
	private int daysRemaining;
	private static Password instance; 
	
	private Password () {
		
	}
               	
	public int getDaysRemaining() {
		return daysRemaining;
	}

	public void setDaysRemaining(int daysRemaining) {
		this.daysRemaining = daysRemaining;
	}

	public String getPasswd() {
		return passwd;
	}
	
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	
	public Date getData_creation() {
		return data_creation;
	}
	
	public void setData_creation(Date data_creation) {
		this.data_creation = data_creation;
	}
	
	public Date getData_remove() {
		return data_remove;
	}
	
	public void setData_remove(Date data_remove) {
		this.data_remove = data_remove;
	}
	
	public static Password getInstance () {
		
		if (instance == null) {
			
			instance = new Password();
		}
		
		return instance;
	}
	
	public boolean insertNewPassword (Password newPassword) throws SQLException, ClassNotFoundException {
		
		Connection conn = Conn.getInstance();
		Statement stmt = conn.createStatement();
		
		String sql = "INSERT INTO madmac.passwords (passwd, data_creation, data_remove) ";
		sql += "VALUES ('" + newPassword.getPasswd() + "', '" + newPassword.getData_creation() + "', ";
		sql += "'" + newPassword.getData_remove() + "') ";
		
		int count = stmt.executeUpdate(sql);
		boolean resultado = false;
		if (count == 1) {
			resultado = true;
		}
		
		return resultado;
	}
	
	public Password selectLastData () throws SQLException, ClassNotFoundException {
		
		Password lastData = new Password();
		
		Connection conn = Conn.getInstance();
		Statement stmt = conn.createStatement();
		
		String sql = "SELECT MAX(data_creation) AS last_data_creation, ";
			  sql += "MAX(data_remove) AS last_data_remove ";
			  sql += "FROM madmac.passwords ";
		
		ResultSet rs = null;
		try {
			
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			
			Conn.restartConnection();
			return null;
		}
		
		if (rs.next()) {
			
			lastData.setData_creation(rs.getDate("last_data_creation"));
			lastData.setData_remove(rs.getDate("last_data_remove"));
		}
		
		return lastData;
	}
	
	public Password selectDaysRemaining () throws SQLException, ClassNotFoundException {
		
		Password lastData = new Password();
		
		Connection conn = Conn.getInstance();
		Statement stmt = conn.createStatement();
		
		String sql = "SELECT data_remove - current_date AS days_remaining ";
		      sql += "FROM madmac.passwords ";
		      sql += "WHERE current_date BETWEEN data_creation AND data_remove ";
		      sql += "AND data_remove = (SELECT MAX(data_remove) FROM passwords) ";
			  
			  /*SELECT data_remove - current_date AS days_remaining FROM passwords
			  WHERE current_date BETWEEN data_creation AND data_remove
			  AND data_remove = (SELECT MAX(data_remove) FROM passwords);*/
		
		ResultSet rs = null;
		try {
			
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			
			Conn.restartConnection();
			return null;
		}
		
		if (rs.next()) {
			
			lastData.setDaysRemaining(rs.getInt("days_remaining"));
		}
		
		return lastData;
	}
	
	public boolean validatePassword (Password verifyPasswd) throws SQLException, ClassNotFoundException {
		
		java.util.Date dataAtual = new java.util.Date();
		SimpleDateFormat sdt = new SimpleDateFormat("dd/MM/yyyy");
		sdt.format(dataAtual);
		
		Connection conn = Conn.getInstance();
		Statement stmt = conn.createStatement();
		
		String sql = "SELECT passwd FROM madmac.passwords ";
		sql += "WHERE '" + dataAtual + "' BETWEEN data_creation AND data_remove ";
		sql += "AND passwd = '" + verifyPasswd.getPasswd() + "' ";
		
		ResultSet rs = null;
		try {
			
      rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			
      Conn.restartConnection();
      return false;
		}
		
		if (rs.next()) {
			
       return true;	
		} else {
			
       return false;			
		}		
	}

}
